 //if(state == "1"){
	// var today =changeDate(ym);//更新日期 
// }else{
	 var today =changeDate(days);//当前日期
// }
 
 var todays =changeDate(days);
 
 


/**初始化日期函数
 * @return
 */
function init(to) {
	initContant();
	var FullYear = to.getFullYear(); //获取年份
	var m = to.getMonth(); //获取月号
	var month = to.getMonth() + 1; //获取月份
	if (month < 10) {
		month = "0" + month;
	}
	var result = ajaxDateTime(FullYear+'-'+month);//加载每月酒店价格
	for ( var i = 0; i < 42; i++) {
		$('<li><p></p></li>').appendTo(".month-body").addClass(
				"month-cell mon");
		var date = to.getDate(); //获取日期
		var day = to.getDay(); //获取星期
		var monthsNum = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
		var isleapyear = FullYear % 4; //判断闰年
		if (isleapyear == 0) {
			monthsNum[1] = 29;
		}
		if (day == 0) {
			day = 7;
		}
		var firstDay = day - (date % 7 - 1); //!important 计算月初星期数
		if (firstDay == 7) { //如果月初为七，归零
			firstDay = 0;
		}
		if (firstDay < 0) { //如果月初为负，加七循环
			firstDay += 7;
		}
		  var f = firstDay;
		  for(var j=1;j<=monthsNum[m];j++){
			  if(j < 10){
	    		  j= '0'+j;
	    	  }
			  var font = FullYear+'-'+month+'-'+j;
			  var pr= "";
			  var ea = "";
			  var si = "";
			  if(result[font] == "undefined" || result[font] == "" || result[font] ==null){
			  }else{
				  pr = result[font].price;
				  ea = result[font].eat;
				  si = result[font].size;
			  }
			  var s = createContant(j,pr,ea,si);
			  //$("li.month-cell span").eq(f).html(j+'<div class="inputs">'+t+'</div>').parent().addClass("pink");
			  $("li.month-cell p").eq(f).html(s).parent().addClass("pink");
			  s="";
			  var ssss= firstDay-1+date;
		      if(ssss <= f){
		    	  $("li.month-cell p").eq(f).parent().attr("data",font).addClass("live").attr("price",pr).attr("eat",ea).attr("size",si);
		      }
		      f++; 
		  }
		  var mm = todays.getMonth() + 1; //获取月份
			if (mm < 10) {
				mm = "0" + mm;
			}
		  if(mm == month){
			  $("li.month-cell p").eq(firstDay-1+date).parent().addClass("red");
		  }
		  else{
			  $("li.month-cell p").eq(firstDay-1+date).parent().addClass("pink");
		  }
		  $(".month-head .s").text(FullYear+"年"+month+"月");
	}
}

/**
 * 创建容器
 * @return
 */
function createContant(day,price,breakfast,size){
	if(price == "" || price == undefined){
		price = "&nbsp;&nbsp;";
	}else{
		price ='¥'+price; 
	}
	if(breakfast == "" || breakfast == undefined){
		breakfast = "无早餐";
	}
	if(size == "" || size == undefined || size == "0"){
		size ="";
		breakfast="";
		price="<font color='red'>无房</font>";
	}else{
		size =size+"间";
	}
	var html = '<b class="date">'+day+'</b>'+
	'<span class="mark pos"></span>'+
	'<span class="price">'+price+'</span>'+
	'<span class="status"><span class="breakfast">'+breakfast+'</span><span class="size">'+size+'</span></span>';
	return html;
}

/**
 * 初始化容器
 * @return
 */
function initContant() {
	$(".mon").remove();
}

function changeMonth(fa) {
	 $("#pl").removeClass("show");
	 $("#pl").addClass("hidden");  
	var FullYear = today.getFullYear(); //获取年份
	var m = today.getMonth(); //获取月号
	if (fa == "next") {
		var month = today.getMonth() + 1 + 1; //获取月份
		if (month > 12) {
			FullYear = FullYear + 1;
			month = 1;
		}
		var ss = changeDate(FullYear+'-'+month+'-01');
		today = ss;
		if ((todays.getMonth()+1) == month) {
			init(todays);
		}else{
			init(ss);
		}
	} else if (fa == "first") {
		var month = today.getMonth() + 1 - 1; //获取月份
		if (month < 1) {
			FullYear = FullYear - 1;
			month = 12;
		}
		var sss = changeDate(FullYear+'-'+month+'-01');
		today = sss;
		if ((todays.getMonth()+1) == month) {
			init(todays);
		}else{
			init(sss);
		}
	}
}


function changeDate(data){
	var date= new Date(Date.parse(data.replace(/-/g,   "/"))); //转换成Data();
	return date;
}

function initArray(d){
	var s = changeDate(d);
	var sd = s.getDay()+1;
	return sd;
}

function ajaxDateTime(datetime){
	var result='';
	var $ids = $("input[name='id']");
	$.ajax({
		type: "GET",
		async : false,
        url: "findPrice.action?id="+$ids.val()+"&datetime="+datetime,
        dataType: "json",
        success:function(data){
		result = data;
        }
     });
	return result;
}

$(function() {
	//绘制月历体
	init(today);
})
